/* b_test_attrition.do - ******************************************************

	Computes test attrition rates for MCAS by grade and cohort, used in Data Appendix
	Table B2

******************************************************************************/
set more off
local grades "K1"
local years "1997 1998 1999 2000 2001 2002 2003"
local mcas_years "3 4 5 6 7 8 10"
local mcas_tests "e m"

local mcas_att = 1
use "$stata_data_crosswalks/SASID_Studentno.dta", clear
keep studentno sasid
save "$stata_data_crosswalks/SASID_Studentno.dta", replace
if `mcas_att' == 1{
	foreach mcas_test of local mcas_tests {
		foreach g of local grades {

		*We want a matrix foreach grade and type of mcas test
		matrix mcas_find_sub = J(17, 17,-1)


		local j = 2

		foreach y of local years {
			local i = 1
			use "$stata_data_analysis/full_samp.dta", clear

			keep if gr_asgn == "`g'" & yr_asgn == `y'

			*Restrict to at risk
			keep if pformany <1 & pformany > 0

			*Store the total number of studentnos this year

			count if in_sims == 1
			matrix mcas_find_sub[`i++', `j'] = r(N)

			foreach mcas_grade of local mcas_years {
				preserve
				replace took`mcas_test'`mcas_grade'=0 if took`mcas_test'`mcas_grade' == .
				di "`mcas_grade'"
				di "`mcas_test'"
				matrix mcas_find_sub[`i',1] = `mcas_grade'

				su took`mcas_test'`mcas_grade' if in_sims == 1
				matrix mcas_find_sub[`i',`j'] = r(mean)

				*balance
				reg took`mcas_test'`mcas_grade' soffany if in_sims == 1, absorb(pformany) r
				matrix mcas_find_sub[`i++',`j'+1] = _b[soffany]
				matrix mcas_find_sub[`i',`j'+1] = _se[soffany]

				restore
				local i = `i' + 1

			}
			local j = `j' + 2

			}
		*Load in the primary named data set
		local i = 1
		use "$stata_data_analysis/full_samp.dta", clear
		keep if gr_asgn == "`g'"
		local i = 1

		*Restrict to at risk
		keep if pformany <1 & pformany > 0

		*Store the total number of studentnos this year

		count if in_sims == 1
		matrix mcas_find_sub[`i++', `j'] = r(sum)
		foreach mcas_grade of local mcas_years {
			preserve
			replace took`mcas_test'`mcas_grade'=0 if took`mcas_test'`mcas_grade' == .
			di "`mcas_grade'"
			di "`mcas_test'"
			matrix mcas_find_sub[`i',1] = `mcas_grade'

			su took`mcas_test'`mcas_grade' if in_sims == 1
			matrix mcas_find_sub[`i',`j'] = r(mean)

			*balance
			reg took`mcas_test'`mcas_grade' soffany i.yr_asgn if in_sims == 1, absorb(pformany) r
			matrix mcas_find_sub[`i++',`j'+1] = _b[soffany]
			matrix mcas_find_sub[`i',`j'+1] = _se[soffany]

			restore
			local i = `i' + 1
		}
		local j = `j' + 2

		matrix mcas_find_sub_transp = mcas_find_sub'
		putexcel set "$results/raw_tabs/table_b2_mcas_`g'_`mcas_test'_sub", replace
		putexcel A1 = matrix(mcas_find_sub_transp)

		}
	}
}
